<?php
include('admin.php');
set_page_info('id', 'categories');

if (ACTION == 'new') {
	if (isset($_POST['submit'])) {
		$c = get_post_data();
		
		if (empty($c['name'])) error('Please enter a name for the category.');
		if (empty($c['slug']) || !valid_slug($c['slug'])) $c['slug'] = get_slug($c['name']);
		$result = $mysql->query('SELECT COUNT(*) FROM `'.CATEGORIES.'` WHERE `category_name` = "'.escape($c['name']).'"');
		if ((int)mysql_result($result, 0) > 0) error('That category already exists.');
		
		$c['description'] = clean($_POST['description'], true);
		$result = $mysql->insert(CATEGORIES, array(
			'category_name' => $c['name'],
			'category_slug' => $c['slug'],
			'category_description' => $c['description'],
		));
		
		if ($result === true) header('Location: categories.php#category-'.$mysql->lastInsertId);
		else error('There was an error saving the category.');
	} else {
		set_page_info('title', 'New Category');
		get_header();
?>
		<h2>New Category</h2>
		<form action="categories.php?action=new" method="post">
			<p><label for="name">Name</label>
			<input type="text" name="name" id="name" class="slug" /></p>
			
			<p><label for="name">Slug</label>
			<input type="text" name="slug" id="slug" class="slughere" /></p>
			
			<p><label for="name">Description</label>
			<textarea name="description" id="description" class="small" rows="6" cols="60"></textarea></p>
			
			<p><input type="submit" name="submit" class="button" value="Save" /></p>
		</form>
<?php
		get_footer();
	}
} elseif (ACTION == 'edit') {
	$id = (int)get_global($_GET, 'id');
	$category = get_single_item(array(
		'table' => CATEGORIES,
		'class' => 'category',
		'where' => '`category_id` = "'.$id.'"'
	));
	if (empty($category)) error('That is not a valid category.');
	
	if (isset($_POST['submit'])) {
		$c = get_post_data();
		
		if (empty($c['name'])) $c['name'] = $category->get_name();
		else {
			$result = $mysql->query('SELECT COUNT(*) FROM `'.CATEGORIES.'` WHERE `category_name` = "'.escape($c['name']).'" AND `category_id` != "'.$category->get_id().'"');
			if ((int)mysql_result($result, 0) > 0) error('That category already exists.');
		}
		if (empty($c['slug'])) $c['slug'] = $category->get_slug();
		
		$c['description'] = clean($_POST['description'], true);
		$result = $mysql->update(CATEGORIES, array(
			'category_name' => $c['name'],
			'category_slug' => $c['slug'],
			'category_description' => $c['description']
		), '`category_id` = "'.$category->get_id().'"');
		
		if ($result === true) header('Location: categories.php#category-'.$category->get_id());
		else error('There was an error saving the category.');
	} else {
		set_page_info('title', 'Editing Category: '.$category->get_name());
		get_header();
?>
		<h2>Editing Category: <?php $category->the_name(); ?></h2>
		<form action="categories.php?action=edit&amp;id=<?php $category->the_id(); ?>" method="post">
			<p><label for="name">Name</label>
			<input type="text" name="name" id="name" class="slug" value="<?php $category->the_name(); ?>" /></p>
			
			<p><label for="name">Slug</label>
			<input type="text" name="slug" id="slug" class="slughere" value="<?php $category->the_slug(); ?>" /></p>
			
			<p><label for="name">Description</label>
			<textarea name="description" id="description" class="small" rows="6" cols="60"><?php $category->the_description(false); ?></textarea></p>
			
			<p><input type="submit" name="submit" class="button" value="Save" /></p>
		</form>
<?php
		get_footer();
	}
} elseif (ACTION == 'delete') {
	$id = (int)get_global($_GET, 'id');
	if ($id === 1) error('You can\'t delete the default category.');
	$result = delete_category($id);
	
	if ($result === true) header('Location: categories.php');
	else error('There was an error deleting the category.');
} else {
	$categories = new items(array(
		'table' => CATEGORIES,
		'class' => 'category',
		'order' => '`category_id` DESC'
	));
	set_page_info('title', 'Manage Categories');
	get_header();
?>
		<h2>Manage Categories<span>(<a href="categories.php?action=new">add one?</a>)</span></h2>
		<form action="mass-delete.php?type=categories" method="post">
			<table  width="100%">
				<thead>
					<tr>
						<th><input type="checkbox" /></th>
						<th>Name</th>
						<th>Description</th>
						<th># of Posts</th>
						<th>&nbsp;</th>
					</tr>
				</thead>
				<tfoot>
					<tr>
						<td colspan="5">
							<input type="submit" name="submit" class="button" value="Delete" />
							Page: <?php echo $categories->pagination['page']; ?><?php if ($categories->needsPagination()) { echo ' &mdash; '; $categories->pagination(); } ?>
						</td>
					</tr>
				</tfoot>
				<tbody>
<?php
	if ($categories->total > 0) {
		foreach($categories->items as $category) {
?>
					<tr<?php $category->the_row_info(); ?>>
						<td class="checkbox"><input type="checkbox" name="mass_delete[]" value="<?php $category->the_id(); ?>" /></td>
						<td><?php $category->the_name(); ?></td>
						<td><?php $category->the_description(false); ?></td>
						<td><?php $category->the_post_amount(); ?></td>
						<td class="options"><?php $category->the_options(); ?></td>
					</tr>
<?php
		}
	} else {
?>
					<tr>
						<td colspan="5">No categories found.</td>
					</tr>
<?php } ?>
				</tbody>
			</table>
		</form>
<?php
	get_footer();
}
?>